package com.wzy.auth.controller;

import com.wzy.ucenter.model.po.XcUser;
import com.wzy.ucenter.service.WeChatAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * @Author wzy
 * @Date 2024/1/30 19:54
 * @description: 微信登录接口
 */
@Slf4j
@Controller
public class WeChatLoginController {
    @Autowired
    private WeChatAuthService weChatAuthService;

    /**
     * 微信登录
     * @param code 微信授权码
     * @param state 状态
     * @return
     * @throws IOException
     */
    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state) throws IOException {
        log.debug("微信扫码回调,code:{},state:{}",code,state);
        //请求微信申请令牌，拿到令牌查询用户信息，将用户信息写入本项目数据库
        XcUser xcUser = weChatAuthService.wxAuth(code);//通过微信授权码查询微信用户信息
        if(xcUser==null){
            return "redirect:http://www.51xuecheng.cn/error.html";
        }
        String username = xcUser.getUsername();
        return "redirect:http://www.51xuecheng.cn/sign.html?username="+username+"&authType=wx";
    }

}
